package com.riatimes.manager
{
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	

	/**
	 * 始终管理器
	 * @author Nick Wong
	 */
	public class ClockManager
	{
		
		private var timerDic:Dictionary=new Dictionary();//每２０毫秒触发函数字典
		private var timer:Timer=new Timer(20);//timer
		private var count:uint=0;//计数器
		/**
		 * 启动时钟
		 */
		public function start():void{
			timer.start();
		}
		
		/**
		 * 停止时钟
		 */
		public function stop():void{
			timer.stop();
		}
		
		/**
		 *timer触发函数 
		 * @param e
		 * 
		 */		
		private function __timerHandle(e:TimerEvent):void{
			count+=1;
			for each(var func:Function in timerDic){
				func();
			}
		
		}
		
		/**
		 * 添加到每２０毫秒触发字典
		 * @param key
		 * @param func
		 */
		public function  addTimerItem(key:*,func:Function):void{
			if(!timerDic[key])
			timerDic[key]=func;
		}
		
		/**
		 *是否存在key 
		 * @param key
		 * @return 
		 * 
		 */		
		public function hasKey(key:*):Boolean{
			if(timerDic[key])return true;
			return false;
		}
		
	
		
		/**
		 * 从每２０毫秒字典中移除指定项
		 * @param key
		 */
		public function removeTimerItem(key:*):void{
			if(timerDic[key])
				delete timerDic[key];
		}
		
		
		
		private static var instance:ClockManager;
		/**
		 * 获取实例对象
		 * @return 
		 */
		public static function getInstance():ClockManager{
			if(instance==null)instance=new ClockManager();
			return instance;
		}
		
		/**
		 * 构造函数
		 */
		public function ClockManager(){
			timer.addEventListener(TimerEvent.TIMER,__timerHandle);
		}
		
	}
}